//
//  ListCentroidesViewController.m
//  PracticoDAW
//
//  Created by Juan Jose Alcazar Cobano on 24/05/13.
//  Copyright (c) 2013 Juan Jose Alcazar Cobano. All rights reserved.
//

#import "ListCentroidesViewController.h"
#import "Centroide.h"
#import "DejalActivityView.h"
#import "DetailsCentroideViewController.h"
#import "LoginViewController.h"

@interface ListCentroidesViewController ()

@end

@implementation ListCentroidesViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        UINavigationItem *navigationItem = [self navigationItem];
        [navigationItem setTitle:@"Centroides"];
        
        UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithTitle:@"Salir" style:UIBarButtonItemStyleDone target:self action:@selector(logout)];
        [navigationItem setRightBarButtonItem:logoutButton];
    }
    return self;
}

- (void)logout
{
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self presentViewController:loginVC animated:YES completion:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.28 green:0.69 blue:0.76 alpha:1.0];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    
    responseData = [[NSMutableData alloc] init];
    allCentroides = [[NSMutableArray alloc] init];
    
    [self createStaticDataList];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([allCentroides count] == 0) {
        return 0;
    } else {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [allCentroides count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    Centroide *l = [allCentroides objectAtIndex:[indexPath row]];
    
    [[cell textLabel] setText:[l idCentroide]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailsCentroideViewController *detailViewController = [[DetailsCentroideViewController alloc] init];
    
    Centroide *l = [allCentroides objectAtIndex:[indexPath row]];
    
    [detailViewController setCentroide:l];
    
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}

- (void)createStaticDataList
{
    Centroide *lat = [[Centroide alloc] init];
    [lat setIdCentroide:@"54930"];
    [lat setLatitude:@"37.375371"];
    [lat setLongitude:@"-5.986261"];
    [lat setNumVeces:@"5"];
    [lat setUsuario:@"alcazar"];
    
    Centroide *lat2 = [[Centroide alloc] init];
    [lat2 setIdCentroide:@"72200"];
    [lat2 setLatitude:@"37.360794"];
    [lat2 setLongitude:@"-5.986261"];
    [lat2 setNumVeces:@"2"];
    [lat2 setUsuario:@"alcazar"];
    
    NSArray *objects = [NSArray arrayWithObjects:lat, lat2, nil];
    
    [allCentroides addObjectsFromArray:objects];
    
    [[self tableView] reloadData];
    
    [DejalBezelActivityView removeView];
}

@end
